import { defineStore } from 'pinia'
import uniStorage from '@/utils/storage'
import type { pickupLocationInterface, shopType, citydateInterface, citydateType, shopInterface } from '@/types'
import { getPickCity, getShop, getSelectShop } from '@/api'
export const pickupStore = defineStore({
  id: 'pickup',
  state: () => ({
    currentCity: '' as string, // 当前选中的城市
    currentCityIndex: 0 as number, // 当前选中的城市索引
    citydate: [] as citydateType, // 城市的列表
    currentShop: {} as shopInterface, // 当前选中的店铺
  }),
  actions: {
    // 获取城市列表
    async getCity() {
      const res = await getPickCity()
      console.log('获取的城市res===', res)
      if (!this.currentCity) {
        this.currentCity = res.data[0].regionName
        this.currentCityIndex = 0
      }
      this.citydate = res.data
    },
    // 获取当前城市下的所有提货点超市
    async selectShop(leaderId: number) {
      await getSelectShop({ leaderId })
      // const homeStore = useHomeStore();
      // homeStore.getHomeIndexAction();
    },
  },
  getters: {
    // 判断是否是当前的提货商店
    isCurrentShop: (state) => {
      return (leaderId: number) => {
        if (!state.currentShop.id) return false
        return state.currentShop.id === leaderId
      }
    },
  },
  // 持久化存储配置
  persist: {
    enabled: true,
    strategies: [
      {
        storage: uniStorage,
      },
    ],
  },
})